package com.ai.service.made.impl;

import com.ai.check.ServerCheck;
import com.ai.common.CommonResponse;
import com.ai.entity.made.camera.CameraPO;
import com.ai.entity.server.ServerBO;
import com.ai.entity.server.ServerPO;
import com.ai.entity.server.ServerPage;
import com.ai.entity.server.ServerVO;
import com.ai.mapper.made.CameraMapper;
import com.ai.mapper.made.ServerMapper;
import com.ai.service.made.ServerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2020-06-17
 */
@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, ServerPO> implements ServerService {

    @Autowired
    CameraMapper cameraMapper;

    /**
     * 通过ID查询
     *
     * @param serverBO
     * @return
     */
    @Override
    public CommonResponse oneById(ServerBO serverBO) {
        ServerPO serverPO = getById(serverBO.getId());
        ServerVO serverVO = ServerVO.convertPOToVO(serverPO);
        return CommonResponse.ok(serverVO);
    }

    /**
     * 修改信息 state 无法修改，maxNum 无法修改
     * 非空校验
     * currentId，id
     * <p>
     * 修改前准备，下面的非空判断前提是 state 必须>0 ,id  不等于当前被修改 id
     * 如果 nickname 不为空，判断是否重复，重复则返回：nickname 已存在
     * 如果 code 不为空，判断是否重复，重复则返回：code 已存在
     * 如果 ip 不为空，判断是否重复，重复则返回：ip 已存在
     *
     * @param serverBO
     * @return
     */
    @Override
    public CommonResponse modifyInfo(ServerBO serverBO) {
        // 权限 以及字段校验 交由注解 进行处理
//        ServerCheck serverCheck = new ServerCheck();
//        if (serverCheck.isCheckID(serverBO)) {
//            return serverCheck.resultData;
//        }
        ServerPO serverPO = null;

        if (!StringUtils.isEmpty(serverBO.getCode())) {
            serverPO = getOne(new QueryWrapper<ServerPO>()
                    .eq("code", serverBO.getCode())
                    .ne("id", serverBO.getId())
                    .gt("state", 0));
            if (serverPO != null) {
                return CommonResponse.error("服务器代码已存在");
            }
        }
        if (!StringUtils.isEmpty(serverBO.getNickname())) {
            serverPO = getOne(new QueryWrapper<ServerPO>()
                    .eq("nickname", serverBO.getNickname())
                    .ne("id", serverBO.getId())
                    .gt("state", 0));
            if (serverPO != null) {
                return CommonResponse.error("服务器名已存在");
            }
        }
        if (!StringUtils.isEmpty(serverBO.getIp())) {
            serverPO = getOne(new QueryWrapper<ServerPO>()
                    .eq("ip", serverBO.getIp())
                    .ne("id", serverBO.getId())
                    .gt("state", 0));
            if (serverPO != null) {
                return CommonResponse.error("服务器IP已存在");
            }
        }

        // TODO: 2023-06-09 修改失败
        serverPO = serverBO.convertBOToPO(serverBO);
        serverPO.setId(Integer.valueOf(serverBO.getId()));
        serverPO.setPort(Integer.valueOf(serverBO.getPort()));
        if (updateById(serverPO)) {
            return CommonResponse.ok("修改成功");
        }
        return CommonResponse.error("修改失败");
    }

    /**
     * 添加
     *
     * @param serverBO
     * @return
     */
    @Override
    public CommonResponse insert(ServerBO serverBO) {
        // 权限 以及字段校验 交由注解 进行处理
//        ServerCheck serverCheck = new ServerCheck();
//        if (serverCheck.isAdd(serverBO)) {
//            return serverCheck.resultData;
//        }
        ServerPO serverPO = null;
        if (!StringUtils.isEmpty(serverBO.getCode())) {
            serverPO = getOne(new QueryWrapper<ServerPO>()
                    .eq("code", serverBO.getCode())
                    .gt("state", 0));
            if (serverPO != null) {
                return CommonResponse.error("服务器代码已存在");
            }
        }
        if (!StringUtils.isEmpty(serverBO.getNickname())) {
            serverPO = getOne(new QueryWrapper<ServerPO>()
                    .eq("nickname", serverBO.getNickname())
                    .gt("state", 0));
            if (serverPO != null) {
                return CommonResponse.error("服务器名已存在");
            }
        }
        if (!StringUtils.isEmpty(serverBO.getIp())) {
            serverPO = getOne(new QueryWrapper<ServerPO>()
                    .eq("ip", serverBO.getIp())
                    .gt("state", 0));
            if (serverPO != null) {
                return CommonResponse.error("服务器IP已存在");
            }
        }
        serverPO = null;
        serverPO = ServerBO.convertBOToPO(serverBO);
        serverPO.setPort(Integer.valueOf(serverBO.getPort()));
        serverPO.setState(1);
        serverPO.setMaxNum(200);
        if (save(serverPO)) {
            return CommonResponse.ok("添加成功");
        }
        return CommonResponse.error("添加失败");
    }

    /**
     * 删除
     *
     * @param serverBO
     * @return
     */
    @Override
    public CommonResponse delete(ServerBO serverBO) {
        // 原本的权限 以及字段校验 交由注解 进行处理
//        ServerCheck serverCheck = new ServerCheck();
//        if (serverCheck.isCheckID(serverBO)) {
//            return serverCheck.resultData;
//        }

        //当服务器绑定摄像头时，不允许删除
        List<CameraPO> cameraPOS = cameraMapper.checkServer(Integer.parseInt(serverBO.getId()));
        if (cameraPOS.size()>0){
            return CommonResponse.error("该服务器存在绑定的摄像头！");
        }

        ServerPO serverPO = new ServerPO();
        serverPO.setId(Integer.parseInt(serverBO.getId()));
        serverPO.setState(-1);
        if (updateById(serverPO)) {
            return CommonResponse.ok("删除成功");
        }
        return CommonResponse.error("删除失败");
    }

    /**
     * 分页查询，列表查询除密码以外的所有用户信息
     *
     * @param page
     * @return
     */
    @Override
    public CommonResponse selectByPage(ServerPage page) {
        QueryWrapper<ServerPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper = page.getQuery(queryWrapper);
        IPage<ServerPO> iPage = page(page, queryWrapper);
        IPage<ServerVO> voIPage = iPage.convert(ServerVO::convertPOToVO);
        return CommonResponse.ok(voIPage);
    }

    /**
     * 全查询
     *
     * @return
     */
    @Override
    public CommonResponse listByOpen() {
        List<ServerPO> cameraPOList = list(new QueryWrapper<ServerPO>()
                .eq("state", 1));
        List<ServerVO> serverVOList = new ArrayList<>();
        cameraPOList.forEach(cameraPO -> {
            ServerVO serverVO = new ServerVO();
            BeanUtils.copyProperties(cameraPO, serverVO);
            serverVOList.add(serverVO);
        });
        return CommonResponse.ok(serverVOList);
    }
}
